package com.jinsilu.freight.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.airbnb.lottie.LottieAnimationView;
import com.blankj.utilcode.util.Utils;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.common.MyActivity;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.CustomerPhoneApi;
import com.jinsilu.freight.other.AppConfig;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.other.SpannableStringUtils;
import com.jinsilu.freight.other.SsbUtils;
import com.jinsilu.freight.other.TouchableSpan;
import com.jinsilu.freight.ui.dialog.AgreementMessageDialog;
import com.jinsilu.freight.ui.dialog.MessageDialog;
import com.tencent.bugly.crashreport.CrashReport;
import com.youth.banner.util.LogUtils;

import okhttp3.Call;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 闪屏界面
 */
public final class SplashActivity extends MyActivity {

    private LottieAnimationView mLottieView;
    private View mDebugView;
    private AppCompatTextView mTvSplashName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setBackgroundDrawable(null);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.splash_activity;
    }

    @Override
    protected void initView() {

        mLottieView = findViewById(R.id.iv_splash_lottie);
        mDebugView = findViewById(R.id.iv_splash_debug);
        mTvSplashName = findViewById(R.id.tv_splash_name);
        // 设置动画监听
        mLottieView.addAnimatorListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                goToMainActivity();
            }
        });
    }

    @Override
    public void onStart(Call call) {
        // 去掉接口调用对话框
    }

    @Override
    public void onEnd(Call call) {
        // 去掉接口调用对话框
    }

    @Override
    protected void initData() {
        if (AppConfig.isDebug()) {
            mDebugView.setVisibility(View.VISIBLE);
        } else {
            mDebugView.setVisibility(View.INVISIBLE);
        }

        if (AppConfig.getPackageName().equals("com.jinsilu.jiuding")) {
            mLottieView.setVisibility(View.GONE);
            mTvSplashName.setVisibility(View.GONE);
        }

//        if (CommonHelper.getAppAuthStatus()) {
//            goToMainActivity();
//        } else {
//            SpannableStringBuilder spannableStringBuilder = SpannableStringUtils
//                    .getBuilder("1.我们会遵循隐私政策收集、使用信息，但不会仅因同意本隐私政策而采取强制捆绑的方式收集信息；")
//                    .append("\n")
//                    .append("2.摄像头、相册权限均不会默认开启，只有经过明示授权才会在为实现功能或服务时使用，不会在功能或服务不需要时而通过您授权的权限收集信息。")
//                    .append("\n")
//                    .append("完整版")
//                    .append("《用户隐私协议》")
//                    .setForegroundColor(Color.parseColor("#3265F9"))
//                    .setClickSpan(new TouchableSpan(Color.parseColor("#3265F9"), Color.parseColor("#3265F9")) {
//                        @Override
//                        public void onClick(@NonNull View widget) {
//                            BrowserActivity.start(SplashActivity.this, MTLConstants.USER_PRIVACY_AGREEMENT);
//                        }
//                    })
//                    .append("及")
//                    .append("《用户服务协议》")
//                    .setForegroundColor(Color.parseColor("#3265F9"))
//                    .setClickSpan(new TouchableSpan(Color.parseColor("#3265F9"), Color.parseColor("#3265F9")) {
//                        @Override
//                        public void onClick(@NonNull View widget) {
//                            BrowserActivity.start(SplashActivity.this, MTLConstants.USER_SERVICE_AGREEMENT);
//                        }
//                    })
//                    .append("。")
//                    .append("\n")
//                    .append("如果您同意，请点击下面的同意按钮以接受我们。")
//                    .create();
//            new AgreementMessageDialog.Builder(this)
//                    // 标题可以不用填写
//                    .setTitle("个人信息保护指引")
//                    // 内容必须要填写
//                    .setMessage(spannableStringBuilder)
//                    // 确定按钮文本
//                    .setConfirm("同意")
//                    .setCancel("不同意")
//                    // 设置 null 表示不显示取消按钮
//                    .setCanceledOnTouchOutside(false)
//                    .setCancelable(false)
//                    // 设置点击按钮后不关闭对话框
//                    //.setAutoDismiss(false)
//                    .setListener(new AgreementMessageDialog.OnListener() {
//                        @Override
//                        public void onConfirm(BaseDialog dialog) {
//                            CommonHelper.saveAppAuthStatus(true);
//                            // Bugly 异常捕捉
//                            CrashReport.initCrashReport(Utils.getApp(), AppConfig.getBuglyId(), AppConfig.isDebug());
//                            goToMainActivity();
//                        }
//
//                        @Override
//                        public void onCancel(BaseDialog dialog) {
//                            AgreementMessageDialog.OnListener.super.onCancel(dialog);
//                            finish();
//                        }
//                    })
//                    .show();
//        }

        // 获取客服电话
        EasyHttp.get(this)
                .api(new CustomerPhoneApi())
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        CommonHelper.saveCustomerPhone(data.getMessage());
                    }
                });
    }

    private void goToMainActivity() {
        if (CommonHelper.isLogin())
            startActivity(HomeActivity.class);
        else
            startActivity(LoginActivity.class);
        finish();
    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return super.createStatusBarConfig()
                // 隐藏状态栏和导航栏
                .hideBar(BarHide.FLAG_HIDE_BAR);
    }

    @Override
    public void onBackPressed() {
        //禁用返回键
        //super.onBackPressed();
    }

    @Override
    public boolean isSwipeEnable() {
        return false;
    }

    @Override
    protected void onDestroy() {
        mLottieView.removeAllAnimatorListeners();
        super.onDestroy();
    }
}